package com.hk.Mgr.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hk.Mgr.web.message.StatusCode;
import com.hk.Mgr.web.message.sys.SysResourceReq;
import com.hk.Mgr.web.message.sys.SysResourceRes;
import com.hk.base.BaseCtrl;
import com.hk.base.ResponseBase;
import com.hk.commons.HqlFilter;
import com.hk.commons.ReqToModelHelper;
import com.hk.commons.entity.sys.SysResource;
import com.hk.service.sys.ISysResource;




/**
 * @author psx 系统资源实现类
 */
@Controller
@RequestMapping(value = BaseCtrl.MgrRootPath + "/SysResourceCtrl")
public class SysResourceCtrl extends BaseCtrl{

	@Autowired
	private ISysResource sysResourceService;

	/**
	 * @Title: getSysResourceById
	 * @Description: 根据id获取系统资源信息
	 * @param
	 */
	@ResponseBody
	@RequestMapping(value = "/getSysResourceById", method = RequestMethod.POST)
	public ResponseBase<SysResourceRes> getSysResourceById(
			@RequestBody SysResourceReq req) {
		ResponseBase<SysResourceRes> resp = new ResponseBase<SysResourceRes>();
		try {
			if (req == null || req.getId() == null) {
				resp.setMsg("参数错误，id为空");
				resp.setResultCode(StatusCode.PARAM_ERROR.key());
				logger.error("method[getSysResourceById]:id is null.");
				return resp;
			}
			SysResource sysResource = sysResourceService.findById(req.getId());
			SysResourceRes singleResp=new SysResourceRes();
			ReqToModelHelper.copyReqValueToModel(sysResource, singleResp);
			SysResource pobj = sysResourceService.findById(sysResource.getParentId());
			if(pobj!=null){
				singleResp.setParentName(pobj.getName());
			}			
			resp.setResult(singleResp);
			resp.setResultCode(StatusCode.SUCCESS.key());
			resp.setMsg("查询成功");
		} catch (Exception ex) {
			logger.error(ex.getMessage(),ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}

	/**
	 * @Title: getSysResourceList
	 * @Description: 分页获取系统资源
	 * @param
	 */
	@ResponseBody
	@RequestMapping(value = "/getSysResourceList", method = RequestMethod.POST)
	public ResponseBase<List<SysResourceRes>> getSysResourceList(
			@RequestBody SysResourceReq req) {
		ResponseBase<List<SysResourceRes>> resp = new ResponseBase<List<SysResourceRes>>();
		HqlFilter hqlFilter=new HqlFilter();
		if(StringUtils.isNotEmpty(req.getName())){
			hqlFilter.addLKFilter("name", req.getName());
		}
		if(StringUtils.isNotEmpty(req.getDescp())){
			hqlFilter.addLKFilter("descp", req.getDescp());
		}
		hqlFilter.addSort("id");
		hqlFilter.addOrder("desc");
		try {
			List<SysResourceRes> respList=new ArrayList<SysResourceRes>();
			List<SysResource> retList = sysResourceService.findByFilter(hqlFilter,
					req.getCurPage(), req.getPageSize());
			if(retList!=null&&!retList.isEmpty()){
				SysResourceRes singleResp=null;
				
				for(SysResource s:retList){
					String parentName="";
					singleResp=new SysResourceRes();
					ReqToModelHelper.copyReqValueToModel(s, singleResp);
					SysResource pobj = sysResourceService.findById(s.getParentId());
					if(pobj!=null)
						parentName=pobj.getName();
					singleResp.setParentName(parentName);
					respList.add(singleResp);
				}
				
				Long total =sysResourceService.countByFilter(hqlFilter);
				resp.setSize(total);
			}
			resp.setResultCode(StatusCode.SUCCESS.key());
			resp.setMsg("查询成功");
			resp.setResult(respList);
		} catch (Exception ex) {
			logger.error(ex.getMessage(),ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}

	/**
	 * @Title: saveSysResource
	 * @Description: TODO
	 * @param
	 */
	@ResponseBody
	@RequestMapping(value = "/saveSysResource", method = RequestMethod.POST)
	public ResponseBase<SysResource> saveSysResource(@RequestBody SysResourceReq req) {
		ResponseBase<SysResource> resp = new ResponseBase<SysResource>();
		try {
			SysResource model = new SysResource();
			ReqToModelHelper.copyReqValueToModel(req, model);
			model.setResType(req.getResType());
			
			Long retId = sysResourceService.add(model);
			if (retId != null) {
				model.setId(retId);
				resp.setResult(model);
				resp.setResultCode(StatusCode.SUCCESS.key());
				resp.setMsg("添加成功");
			} else {
				resp.setResultCode(StatusCode.ERROR.key());
				resp.setMsg("添加失败");
			}
		} catch (Exception ex) {
			logger.error(ex.getMessage(),ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}

	/**
	 * @Title: updateSysResource
	 * @Description: TODO
	 * @param
	 */
	@ResponseBody
	@RequestMapping(value = "/updateSysResource", method = RequestMethod.POST)
	public ResponseBase<SysResource> updateSysResource(
			@RequestBody SysResourceReq req) {
		ResponseBase<SysResource> resp = new ResponseBase<SysResource>();
		try {
			if (req == null || req.getId() == null) {
				resp.setResultCode(StatusCode.PARAM_ERROR.key());
				resp.setMsg("参数不对：id is null！");
				return resp;
			}
			SysResource model = sysResourceService.findById(req.getId());
			ReqToModelHelper.copyReqValueToModel(req, model);
			model.setResType(req.getResType());
			
			sysResourceService.saveOrUpdate(model);
			resp.setResultCode(StatusCode.SUCCESS.key());
			resp.setMsg("修改成功");
			resp.setResult(model);
		} catch (Exception ex) {
			logger.error(ex.getMessage(),ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}

	/**
	 * @Title: deleteSysResource
	 * @Description: TODO
	 * @param
	 */
	@ResponseBody
	@RequestMapping(value = "/deleteSysResource", method = RequestMethod.POST)
	public ResponseBase<SysResource> deleteSysResource(@RequestBody SysResourceReq req) {
		ResponseBase<SysResource> resp = new ResponseBase<SysResource>();
		try {
			if (req == null || req.getId() == null) {
				resp.setResultCode(StatusCode.PARAM_ERROR.key());
				resp.setMsg("参数不对：id is null！");
				return resp;
			}
			SysResource ret = sysResourceService.delete(req.getId());
			resp.setResultCode(StatusCode.SUCCESS.key());
			resp.setMsg("删除成功");
			resp.setResult(ret);
		} catch (Exception ex) {
			logger.error(ex.getMessage(),ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}

}
